﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchool.Models;
using System.Web.Security;
using System.Diagnostics;
using System.IO;
using GameSchool.ViewModels;

namespace GameSchool.Controllers
{
    public class AssignmentSolutionController : Controller
    {
		AssignmentSolutionRepository repository = new AssignmentSolutionRepository();

		// Skila öllum lausnum
        public ActionResult Index()
        {
			var items = from n in repository.AssignmentSolutions()
						select n;
			return View(items);
        }

		// Þessi aðgerð sér um að hlaða inn lausn nemanda
		[HttpPost]
		public ActionResult AddAssignmentSolution(HttpPostedFileBase file, ViewModels.ViewAssignmentViewModel va)
		{
			AssignmentSolution newAssSol = new AssignmentSolution();
			var UserId = new Guid(Membership.GetUser(HttpContext.User.Identity.Name).ProviderUserKey.ToString());

			// Athuga hvort nemandinn sé búinn að senda inn lausn áður
			var alreadyExists = false;
			foreach (var n in repository.AssignmentSolutions())
			{
				if (n.UserID == UserId && n.AssignmentID == va.gameID)
				{
					newAssSol = n;
					alreadyExists = true;
				}
			}

			// Setja inn nýjar upplýsingar
			var fullFileUrl = ("User_Uploads/Assignments/" + UserId + "/" + va.gameID + "/" + file.FileName).ToString();
			newAssSol.AssignmentID = va.gameID;
			newAssSol.FileName = file.FileName;
			newAssSol.FileUrl = fullFileUrl;
			newAssSol.Date = DateTime.Now;
			newAssSol.UserID = UserId;
			newAssSol.UserName = User.Identity.Name;

			// Ef nemandi hefur ekki sent inn lausn áður, þá búa til nýja færslu í grunninum og gefa stig
			if (alreadyExists != true)
			{
				// Finna rétt verkefni
				AssignmentRepository aRep = new AssignmentRepository();
				var model = new ViewAssignmentViewModel
				{
					// Finna verkefnið sem lausnin á við
					Assignment = (from n in aRep.Assignments()
								  where n.ID == va.gameID
								  select n).SingleOrDefault(),

				};
				// Og gefa nemandanum stig fyrir að hafa skilað inn lausn á verkefninu
				UserPointsRepository uRep = new UserPointsRepository();
				var uPoints = (from n in uRep.UserPoints()
							  where n.UserId == UserId
							  select n).SingleOrDefault();
				uPoints.TotalPoints += model.Assignment.Points;
				uPoints.CurrentPoints += model.Assignment.Points;
				UpdateModel(uPoints);
				uRep.Save();

				repository.Add(newAssSol);
			}

			// Vista breytingar
			UpdateModel(newAssSol);
			repository.Save();

			// Staðfesta að notandinn hafi valið skrá
			if (file != null && file.ContentLength > 0)
			{
				// ná í skráarnafnið
				var fileName = Path.GetFileName(file.FileName);
				var folder = Server.MapPath("~/User_Uploads/Assignments/" + UserId + "/" + va.gameID);
				Directory.CreateDirectory(folder);
				// geyma skrána í ~/User_Uploads/Assignments möppunni í undirmöppunum "UserId/GameId"
				var path = Path.Combine(Server.MapPath("~/User_Uploads/Assignments/" + UserId + "/" + va.gameID), fileName);
				file.SaveAs(path);
			}

			return RedirectToAction("../Assignment/ViewAssignment/" + newAssSol.AssignmentID);
		}
    }
}
